<?
require('JSON.php');


$json = new Services_JSON();

//foreach($_POST as $key => $value) {
//	error_log(" $key => $value ");
//	
//	foreach ($value as $k => $v) {
//		error_log(" $k => $v ");
//		if( $k == 'items' ) {
//			foreach 
//		}
//	} 
//}

$output = array ();

$output['count'] 		= $_POST['sortable_list']['count'];
$output['firstIndex'] 	= $_POST['sortable_list']['firstIndex'];
$output['columns']		= array('Title(ID)');
//$output['items']		= $_POST['sortable_list']['items'];


foreach ($_POST['sortable_list']['items'] as $key => $value) {
	
	//error_log(" $key => $value ");
	$output['items'][] = addItem($value);
}

$info = $json->encode($output);
file_put_contents('test.js',$info);
error_log($info);



function addInfo($item) {
	
	$item['info'] = array('Page Title('.$item[id].')');
	/*
	if($item['children']) {
		foreach ($item['children'] as $k => $v) {
			$item['children']['items'] = addInfo($v);
		}
	}*/
	
	return $item; 
}

function addItem ($item) {
	
	$new = array();
	
	$new[id] 	= $item[id];
	$new[info]	= array("Page Title($item[id])");
	
	if($item['children']) {
		foreach ($item['children'] as $k => $v) {
			$new['children'][] = addItem($v);
		}
	}
	
	return $new;
}
?>